<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :okText="$t('m.common.button.save')"
    :cancelText="$t('m.common.button.close')">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.quartzjob.entity.jobName')"
          hasFeedback >
          <a-input placeholder="" v-decorator="['jobName', {rules: [{ required: true, message:this.$t('m.common.tip.verify_required') }]}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.quartzjob.entity.jobClassName')"
          hasFeedback >
          <a-input placeholder="" v-decorator="['jobClassName', {rules: [{ required: true, message:this.$t('m.common.tip.verify_required') }]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.quartzjob.entity.cronExpression')">
          <ych-cron ref="innerVueCron" v-decorator="['cronExpression', { initialValue: '* * * * * ? *' }]" @change="setCorn"></ych-cron>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.quartzjob.entity.parameter')">
          <a-textarea placeholder="" :rows="5" v-decorator="['parameter', {}]" />
        </a-form-item>
        <!--<a-form-item-->
          <!--:labelCol="labelCol"-->
          <!--:wrapperCol="wrapperCol"-->
          <!--label="描述">-->
          <!--<a-textarea placeholder="请输入描述" :rows="3" v-decorator="['description', {}]" />-->
        <!--</a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.quartzjob.entity.status')">
          <ych-dict-select-tag type="radioButton"  v-decorator="[ 'status', {'initialValue':0}]" :trigger-change="true" dictCode="quartz_status"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";
  import {YchCron} from '@yuncheng/ych-components'

  export default {
    name: "QuartzJobModal",
    components:{
      YchCron
    },
    data () {
      return {
        title:"操作",
        buttonStyle: 'solid',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        cron: {
          label: '',
          value: ''
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          cron: {
            rules: [{
              required: true, message: this.$t('m.common.tip.verify_required')
            }]
          }
        },
        url: {
          add: YchConstant.PREFIX_SYSTEM + "/quartzJob/add",
          edit: YchConstant.PREFIX_SYSTEM + "/quartzJob/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        let that = this;
        that.form.resetFields();
        this.model = Object.assign({},record);
        //console.log(this.model)
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model,'jobName','jobClassName','cronExpression','parameter','status'));
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          //console.log('values',values)
          if (!err) {
            if (typeof values.cronExpression == "undefined" || Object.keys(values.cronExpression).length==0 ) {
              this.messageUtil.warning(this.$t('m.system.quartzjob.verify.cronExpression'));
              return false;
            }

            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            //console.log('提交参数',formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.messageUtil.success(res.message);
                that.$emit('ok');
              }else{
                that.messageUtil.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },
      setCorn(data){
        //console.log('data)',data);
        this.$nextTick(() => {
          this.model.cronExpression = data;
        })

        // //console.log(Object.keys(data).length==0);
        if (Object.keys(data).length==0) {
          this.messageUtil.warning(this.$t('m.system.quartzjob.verify.cronExpression'));
        }
      },
      validateCron(rule, value, callback){
        if(!value){
          callback()
        }else if (Object.keys(value).length==0) {
          callback(this.$t('m.system.quartzjob.verify.cronExpression'));
        }
      },

    }
  }
</script>
